package com.zs.studentmanager.controller;

import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.common.Result;
import com.zs.studentmanager.entity.domain.Course;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.service.CourseService;
import com.zs.studentmanager.utils.CommonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * @author: 周小白
 * @date: 2024-01-22 16:05
 **/
@RestController
@RequestMapping("/course")
@Tag(name= "课程管理")
public class CourseController {


    private final CourseService courseService;

    public CourseController(CourseService courseService) {
        this.courseService = courseService;
    }


    /**
     * 获取课程列表
     * @param pageNum  当前页
     * @param pageSize 每页显示条数
     * @param searchValue 搜索内容
     * @return  课程列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取课程列表")
    public Result getCourseList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                @RequestParam(value = "searchValue", defaultValue = "") String searchValue){
        if (pageNum < 1 || pageSize < 1) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        PageVo<Course> pageVo = courseService.getCourseList(pageNum, pageSize, searchValue);
        if (pageVo == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success(pageVo);
    }


    /**
     * 添加课程
     * @param course 课程信息
     * @return 添加结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加课程")
    @Transactional(rollbackFor = Exception.class)
    public Result addCourse(@RequestBody Course course){
        if (course == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        course.setCreateTime(LocalDateTime.now());
        long currentUserId = CommonUtils.getCurrentUserId();
        course.setCreateBy(currentUserId);

        boolean result = courseService.addCourse(course);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }





    /**
     * 获取课程详情
     * @param id 课程id
     * @return 课程详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取课程详情")
    public Result getCourseDetails(@PathVariable("id") Long id){
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        Course course = courseService.getById(id);
        if (course == null) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success(course);
    }


    /**
     * 修改课程
     * @param course 课程信息
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改课程")
    @Transactional(rollbackFor = Exception.class)
    public Result updateCourse(@RequestBody Course course){
        if (course == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        course.setUpdateTime(LocalDateTime.now());
        long currentUserId = CommonUtils.getCurrentUserId();
        course.setUpdateBy(currentUserId);

        boolean result = courseService.updateById(course);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }


    /**
     * 删除课程
     * @param id 课程id
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除课程")
    public Result deleteCourse(@PathVariable("id") Long id){
        if (id == null) {
            return Result.fail(ResponseCodeEnum.NO_PARAM);
        }

        boolean result = courseService.removeById(id);
        if (!result) {
            return Result.fail(ResponseCodeEnum.SYSTEM_ERROR);
        }

        return Result.success();
    }




}
